package com.nl.controller.web.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nl.controller.web.bean.ChangeData;
import com.nl.controller.web.bean.meteorologicalStateChange.StateChangeInfo;
import com.nl.controller.web.mapper.MeteorologicalStateChangeMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MeteorologicalStateChangeService {
	@Autowired
	private MeteorologicalStateChangeMapper meteorologicalStateChangeMapper;
	
	@Autowired 
	private ChangeSynchronizationInterfaceService changeSynchronizationInterfaceService;
	//@SystemServiceLog(description = "气象局项目状态接口", value = false)
	@Transactional
	public Map<String, Object> meteorologicalStateChange(String stateChangeList){
						Map<String,Object> map=new HashMap<String, Object>();
						String resultCode="-1";
						String resultDesc="";
						int result =0;
						StateChangeInfo stateChangeInfo = null;
						try {
							JSONObject json = JSON.parseObject(stateChangeList);
							if(null!=json && !"".equals(json)){
								log.info("【任务信息同步接口】json：{}",json.toJSONString());
								String taskId = json.getString("taskId");
								int status = Integer.parseInt(json.getString("status"));

								if(status == 1) {
									status = 0;
								}else if(status == 2){
									status = 1;
								}else{
									resultCode="-1"; resultDesc="非暂停、开启状态";
								}
								stateChangeInfo = meteorologicalStateChangeMapper.findBytaskId(taskId);
								if(stateChangeInfo==null){
									resultCode="-1";
									resultDesc = "失败，找不到匹配项目";
									log.info("调用气象局项目状态接口失败");
									map.put("resultCode", resultCode);
									map.put("resultDesc", resultDesc);
					return map;
				}
				if(taskId.equals(stateChangeInfo.getTaskId())){
					stateChangeInfo.setStatus(status);
					result = meteorologicalStateChangeMapper.modifyStatus(stateChangeInfo);
					if(result>0){
						ChangeData changeData = changeSynchronizationInterfaceService.findChangeByMgrId(stateChangeInfo.getId());
						JSONObject json1 = new JSONObject();
			    		changeData.setStreamSeq(changeSynchronizationInterfaceService.getStreamSeq());
			   		 	log.info(" 项目信息：{}", changeData.toString());
			   		 	/*json.put("changeData",changeData);*/
			   		 	json1.put("mgrId",changeData.getMgrId());
			   		 	json1.put("checkState",changeData.getCheckState());
			   		 	json1.put("mgrState",changeData.getMgrState());
			   		 	json1.put("sendStart",changeData.getSendStart());
			   		 	json1.put("sendEnd",changeData.getSendEnd());
			   		 	json1.put("streamSeq",changeData.getStreamSeq());
			   		 	json1.put("flag","program_update");
			   		 	System.out.println("json:"+json1.toString());
			   		 	Map<String, Object> map1 = changeSynchronizationInterfaceService.changeSynchronization(json1);
			   		 	log.info(" 同步IT任务返回信息：{}", map1.toString());
			   		 	if ("0".equals(map1.get("resultCode"))) {
				   		 	resultCode="0";
							resultDesc = "成功";
							log.info("调用状态同步接口成功",taskId);
						}else {
							resultDesc = "同步失败";
							log.info("调用IT同步接口成功",taskId);
						}
					}else{
						resultDesc = "没有数据";
						log.info("调用状态同步接口成功");
					}

				} else{
					resultCode="-1";
					resultDesc = "失败，找不到匹配项目";
					log.info("调用气象局项目状态接口失败");
				}
			}else{
				resultDesc = "未获取到需要同步的任务信息";
			}

		} catch (Exception e) {
			log.info(e.toString());
			resultDesc="气象局项目状态接口异常,error:"+e.getMessage();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();

			log.info("调用气象局项目状态接口失败");
		}
		map.put("resultCode", resultCode);
		map.put("resultDesc", resultDesc);
		return map;
	}
}
